package com.ygqh.baby.controller.admin;

import com.ygqh.baby.ao.*;
import com.ygqh.baby.po.YgGroup;
import com.ygqh.baby.service.YgGroupService;
import com.ygqh.baby.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/group")
public class GroupController extends BaseController<Object, java.lang.Long> {

    @Autowired
    private YgGroupService ygGroupService;

    @RequestMapping("searchGroup")
    @ResponseBody
    public Map<String, Object> searchGroup(String q, QueryInfo queryInfo, GroupType groupType, Boolean isGroup) {

        ResultSet<YgGroup> resultSet = ygGroupService.search(queryInfo, getSession().getSupplierId(), q, groupType, isGroup);

        return resultSet.toDatatables(null);
    }

    @RequestMapping("searchGroupList")
    @ResponseBody
    public List<YgGroup> searchGroupList(@RequestParam(defaultValue = "Web") GroupType groupType) {
        return ygGroupService.find(groupType, null);

    }

    @RequestMapping("saveGroup")
    @ResponseBody
    public Message saveGroup(String strGroup) {
        YgGroup group = JsonUtils.toObject(strGroup, YgGroup.class);

        group.setSupplierId(getSession().getSupplierId());
        group.setCreateBy(getSession().getTrueName());
        group.setCreateTime(new Date());
        // 判断是否含有相同的名字
        if (checkName(group.getGroupName(), group.getId(), group.getGroupType()) > 0)
            return Message.error("款式名称已经存在");
        if (ygGroupService.saveOrUpdate(group) == 1) {
            return Message.success(SUCCESS_MESSAGE.getCode(), group.getId());
        }
        return ERROR_MESSAGE;
    }

    @RequestMapping("updateGroupStatus")
    @ResponseBody
    public Message updateGroupStatus(Long id) {
        YgGroup ygGroup = new YgGroup();
        ygGroup.setId(id);
        ygGroup.setUpdateBy(getSession().getTrueName());
        ygGroup.setUpdateTime(new Date());
        ygGroup.setStatus(DataStatus.Delete);
        return ygGroupService.update(ygGroup) == 1 ? SUCCESS_MESSAGE : ERROR_MESSAGE;
    }

    private int checkName(String name, Long id, GroupType groupType) {
        return ygGroupService.findName(name, id, groupType);
    }

    @RequestMapping("updateGroup")
    @ResponseBody
    public Message updateGroup(Long id, @RequestParam(defaultValue = "0.00") BigDecimal promotionPrice, Long groupAmount, GroupMode groupMode,
                               BigDecimal shareRate, Boolean isReal, BigDecimal vipRate, @RequestParam(defaultValue = "0.00") BigDecimal distributionPrice,Integer groupDate) {
        isReal = isReal == null ? false : isReal;
        if (promotionPrice.compareTo(ZERO) > 0) {
            shareRate = new BigDecimal("0.05");
            vipRate = shareRate;
        } else {
            shareRate = new BigDecimal("0.10");
        }
        YgGroup group = new YgGroup();
        group.setUpdateBy(getSession().getTrueName());
        group.setUpdateTime(new Date());
        group.setId(id);
        group.setGroupAmount(groupAmount);
        group.setPromotionPrice(promotionPrice);
        group.setGroupMode(groupMode);
        group.setShareRate(shareRate);
        group.setVipRate(vipRate);
        group.setIsReal(isReal);
        group.setGroupDate(groupDate);
        group.setDistributionPrice(distributionPrice);
        ygGroupService.updatePromotion(group);
        return SUCCESS_MESSAGE;
    }
    @RequestMapping("getGroupById")
    @ResponseBody
    public Message getGroupById(Long id){
    	return Message.success(ygGroupService.findById(id));
    	
    }

}
